using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using System.Collections.Generic;

public partial class pages_shopping_cart : System.Web.UI.Page
{
    BookDAO daoBook = new BookDAO();
    protected void Page_Load(object sender, EventArgs e)
    {

        if (!IsPostBack)
        {

            if (Request.UrlReferrer != null)
                ViewState["ReferrerUrl"] = Request.UrlReferrer.ToString();
            int item_id = int.Parse(Request["item_id"]);
            CartItem item = new CartItem();

            List<CartItem> shopping_cart = Session["shoppingcart"] as List<CartItem>;
            if (shopping_cart != null)
            {
                IList<Book> selectedBook = daoBook.GetBookByID(item_id);
                if (selectedBook.Count == 0) return;
                item.ItemID = selectedBook[0].BookID;
                item.ItemName = selectedBook[0].Name;
                item.Price = selectedBook[0].Price;
                item.Quantity = 1;
                if (!shopping_cart.Contains(item))
                {
                    shopping_cart.Add(item);

                }
     
                dgrCart.DataSource = shopping_cart;
                dgrCart.DataBind();
            }
        }




    }
    protected void dgrCart_SelectedIndexChanged(object sender, EventArgs e)
    {
        
    }
    protected void dgrCart_RowUpdating(object sender, GridViewUpdateEventArgs e)
    {
        List<CartItem> shopping_cart = Session["shoppingcart"] as List<CartItem>;
        int id=int.Parse(dgrCart.DataKeys[e.RowIndex].Value.ToString());
        TextBox txtQuantity=(TextBox)dgrCart.Rows[e.RowIndex].FindControl("txtQuantity");
        CartItem cItem = new CartItem();
        foreach (CartItem item in shopping_cart)
        {
            if (item.ItemID==id)
            {
                item.Quantity = int.Parse(txtQuantity.Text); 
            }
        }     
   
        Session["shoppingcart"] = shopping_cart;
        dgrCart.EditIndex = -1;
        LoadData();
    }
    protected void dgrCart_RowEditing(object sender, GridViewEditEventArgs e)
    {
        dgrCart.EditIndex = e.NewEditIndex;
        LoadData();
    }
    protected void dgrCart_RowCancelingEdit(object sender, GridViewCancelEditEventArgs e)
    {
        dgrCart.EditIndex = -1;
        LoadData();
        
    }
    public void LoadData()
    {
        List<CartItem> shopping_cart = Session["shoppingcart"] as List<CartItem>;
        dgrCart.DataSource = shopping_cart;
        dgrCart.DataBind();
    }
    protected void dgrCart_RowDeleting(object sender, GridViewDeleteEventArgs e)
    {
        int id = int.Parse(dgrCart.DataKeys[e.RowIndex].Value.ToString());
        List<CartItem> shopping_cart = Session["shoppingcart"] as List<CartItem>;
        CartItem item = new CartItem(id,"",0,0);
        if (shopping_cart.Remove(item))
        {
            Session["shoppingcart"] = shopping_cart;
            dgrCart.EditIndex = -1;
            LoadData();
        }
    }
    protected void lnkContinue_Click(object sender, EventArgs e)
    {
        // Allow user to continue to select another items
        object referrer = ViewState["ReferrerUrl"];
        if (referrer != null)
            Response.Redirect((string)referrer);
        
    }
}
